/*
 * Copyright (C) 2012 artit91
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities.factories;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.HashMap;
import rpg.Events;
import rpg.Maps;
import rpg.entities.Sprite;

/**
 *
 * @author artit91
 */
public abstract class SpriteFactory extends Sprite {

	private static AbstractMap<Integer, SpriteFactory> instances = null;
	private static int unique = 1;

	protected SpriteFactory() {
		super();
	}

	public synchronized static void newInstance(final SpriteFactory instance) {
		if (instances == null) {
			instances = new HashMap<Integer, SpriteFactory>();
		}
		if (instance.getProp().getType().equals("Player")) {
			instance.getProp().setId(0);
			instances.put(0, instance);
		} else {
			instance.getProp().setId(unique);
			instances.put(unique, instance);
			unique++;
		}
	}

	public synchronized static SpriteFactory getInstance(final int i) {
		return instances.get(i);
	}

	public synchronized static void newInstance(final char c, final Maps.Map map, final int loc) {
		if ("z*".contains(String.valueOf(c))) {
			CreatureFactory.newInstance(c, map, loc);
		} else if ("cr".contains(String.valueOf(c))) {
			CritterFactory.newInstance(c, map, loc);
		}
	}

	public synchronized static Collection<SpriteFactory> getEntites() {
		return instances.values();
	}

	public synchronized static void remove(final int id) {
		instances.remove(id);
	}

	public synchronized static void clear() {
		
		if (instances != null) {
			for (SpriteFactory sf : instances.values()) {
				sf.setEh(null);
				sf.setMap(null);
				sf.setProp(null);
			}
			instances.clear();
		}
	}
}
